public class Test15 {
    public static ListNode findKthToTail(ListNode head,int k){

        if(k < 1 || head==null) return null;
        ListNode node1 = head;
        ListNode node2 = head;
        for(int i = 0;i<k-1;i++){
            if(node1 != null)
                node1 = node1.next;
            else return null;
        }
        while(node1.next != null){
            node1 = node1.next;//找到最后一个节点的位置
            node2 = node2.next;
        }
        return node2;
    }

    public static void main(String[] args) {


        ListNode head = new ListNode();
        head.val = 1;

        head.next = new ListNode();
        head.next.val = 2;

        head.next.next = new ListNode();
        head.next.next.val = 3;

        head.next.next.next = new ListNode();
        head.next.next.next.val = 4;

        ListNode middle = head.next.next.next.next = new ListNode();
        head.next.next.next.next.val = 5;

        head.next.next.next.next.next = new ListNode();
        head.next.next.next.next.next.val = 6;

        ListNode last = head.next.next.next.next.next.next = new ListNode();
        head.next.next.next.next.next.next.val = 7;

        System.out.println(findKthToTail(head, 7).val);
        System.out.println(findKthToTail(head, 3).val);

    }
}
